/*
 * 文  件   名: FriendManagerImpl.java
 * 版         本 : docrepository-server(Anttribe). All rights reserved.
 * 描         述 : <描述>
 * 修   改  人: zhaoyong
 * 修改时间: 2014年11月17日
 */
package org.anttribe.docrepository.friend.manager.impl;

import java.util.List;

import org.anttribe.docrepository.common.UnifyException;
import org.anttribe.docrepository.common.errorno.SystemErrorNumber;
import org.anttribe.docrepository.friend.dao.UserFriendDao;
import org.anttribe.docrepository.friend.dao.UserGroupDao;
import org.anttribe.docrepository.friend.domain.UserFriend;
import org.anttribe.docrepository.friend.domain.UserGroup;
import org.anttribe.docrepository.friend.manager.FriendManager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhaoyong
 * @version 2014年11月17日
 */
public class FriendManagerImpl implements FriendManager
{
    
    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(FriendManagerImpl.class);
    
    /**
     * userGroupDao
     */
    private UserGroupDao userGroupDao;
    
    /**
     * userFriendDao
     */
    private UserFriendDao userFriendDao;
    
    @Override
    public List<UserGroup> listUserGroup(String ownerId)
        throws UnifyException
    {
        if (StringUtils.isEmpty(ownerId))
        {
            LOGGER.warn("While querying, ownerId is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        return userGroupDao.queryByOwner(ownerId);
    }
    
    @Override
    public List<UserFriend> listUserFriend(String groupId, String ownerId)
        throws UnifyException
    {
        if (StringUtils.isEmpty(ownerId))
        {
            LOGGER.warn("While querying, ownerId is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        return userFriendDao.queryByOwnerAndGroup(ownerId, groupId);
    }
    
    @Override
    public void editFriendName(String userFriendId, String friendName)
    {
        if (StringUtils.isEmpty(userFriendId))
        {
            LOGGER.warn("While editing user friend Name, userFriendId is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        UserFriend userFriend = new UserFriend();
        userFriend.setUserFriendId(userFriendId);
        userFriend.setFriendName(friendName);
        userFriendDao.update(userFriend);
    }
    
    @Override
    public void removeUserFriend(String userFriendId)
    {
        if (StringUtils.isEmpty(userFriendId))
        {
            LOGGER.warn("While removing user friend Name, userFriendId is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        userFriendDao.deleteById(userFriendId);
    }
    
    public void setUserGroupDao(UserGroupDao userGroupDao)
    {
        this.userGroupDao = userGroupDao;
    }
    
    public void setUserFriendDao(UserFriendDao userFriendDao)
    {
        this.userFriendDao = userFriendDao;
    }
}